package dfs;
/*
* 124. 二叉树中的最大路径和
* */
public class MaxPathSum124 {
    int max=Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        dfs(root);
        return max;
    }
    private int dfs(TreeNode root) {
        if(root==null){
            return 0;
        }
        int left=dfs(root.left);
        int right=dfs(root.right);
        left=left>0?left:0;
        right=right>0?right:0;
        max=Math.max(max,root.val+left+right);
        return root.val+Math.max(left,right);
    }
}
